import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_elf/config/component_index.dart';
import 'package:flutter_elf/widgets/appbar_creator.dart';
import 'package:provider/provider.dart';

void main(){
  runApp(MaterialApp(
    home: ProviderPage(),
  ));
}

class ProviderPage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBarCreator.create(context, '测试页'),
      body: Column(
        children: <Widget>[
          FlatButton(
            onPressed: (){
              NavigatorUtils.navigatorRouter(context, SingleProviderPage());
            },
            child: Text('SingleProviderPage'),
          ),
          FlatButton(
            onPressed: (){
              NavigatorUtils.navigatorRouter(context, MultiProviderPage());
            },
            child: Text('SingleProviderPage'),
          ),
        ],
      ),
    );
  }
}

class SingleProviderPage extends BaseWidget{
  @override
  BaseWidgetState<BaseWidget> getState() {
    // TODO: implement getState
    return ProviderPageState();
  }
}

class ProviderPageState extends BaseWidgetState<SingleProviderPage>{



  @override
  Widget buildView(BuildContext context) {
    // TODO: implement buildView
    return Provider<String>.value(
      value: '1',
      child: Consumer<String>(
        builder: (context,s,child){
          return Column(
            children: <Widget>[
              child,
              Text(s)
            ],
          );
        },
        child: Column(
          children: <Widget>[
            SizedBox(height: px(100),),
            Text('测测试试')
          ],
        ),
      ),
    );
  }

  @override
  PreferredSizeWidget initAppbar() {
    // TODO: implement initAppbar
    return AppBarCreator.create(context, 'SingleProvider');
  }
}

class MultiProviderPage extends BaseWidget{
  @override
  BaseWidgetState<BaseWidget> getState() {
    // TODO: implement getState
    return MultiProviderPageState();
  }
}
class MultiProviderPageState extends BaseWidgetState<MultiProviderPage>{
  @override
  Widget buildView(BuildContext context) {
    // TODO: implement buildView
    return MultiProvider(
        providers: [
          Provider<int>.value(value: 100),
          Provider<String>.value(value: '123'),
          ChangeNotifierProvider<Model>.value(value: Model(1000)),
        ],
      child: Consumer3<int,String,Model>(
        builder: (context,i,s,m,child){
          return Column(
            children: <Widget>[
              child,
              Text(i.toString()),
              Text(s),
              GestureDetector(
                  onTap: (){
                    Provider.of<Model>(context).setIndex(3012);
                  },
                  child: Text(m.getIndex().toString())),
            ],
          );
        },
        child: Column(
          children: <Widget>[
            Text('MultiProviderPage')
          ],
        ),
      ),
    );
  }

  @override
  PreferredSizeWidget initAppbar() {
    // TODO: implement initAppbar
    return AppBarCreator.create(context, 'MultiProvider');
  }

}
class Model extends ChangeNotifier{
  int _index = 100;


  Model(this._index);

  setIndex(int index){
    this._index++;
    notifyListeners();
  }

  getIndex()=>_index;

}